package com.mjk.manager.magicapi.conf;

import com.mjk.common.core.jdbc.datasource.IDataSource;
import com.mjk.manager.magicapi.conf.interceptor.CustomRequestInterceptor;
import com.mjk.manager.magicapi.conf.interceptor.CustomUIAuthorizationInterceptor;
import com.mjk.manager.magicapi.conf.provider.*;
import com.mjk.manager.magicapi.conf.scripts.CustomFunction;
import com.mjk.manager.magicapi.conf.scripts.CustomFunctionExtension;
import com.mjk.manager.magicapi.conf.scripts.CustomModule;
import com.mjk.manager.magicapi.core.backup.service.MagicBackupService;
import com.mjk.manager.magicapi.core.backup.service.MagicDatabaseBackupService;
import com.mjk.manager.magicapi.core.datasource.model.MagicDynamicDataSource;
import com.mjk.manager.magicapi.core.modules.db.provider.PageProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Map;


@Configuration
public class MagicAPIConfiguration {
    /**
     * 配置多数据源
     *
     * @see MagicDynamicDataSource
     */
    @Bean
    public MagicDynamicDataSource magicDynamicDataSource(IDataSource dataSource) {
        MagicDynamicDataSource dynamicDataSource = new MagicDynamicDataSource();
        dynamicDataSource.setDefault(dataSource.getDataSource(null)); // 设置默认数据源
        dynamicDataSource.add("slave", dataSource.getDataSource(null));
        return dynamicDataSource;
    }

    /**
     * 初始化备份数据源
     *
     * @param dataSource
     * @return
     */
    @Bean
    public MagicBackupService customScriptProvider(IDataSource dataSource) {
        return new MagicDatabaseBackupService(dataSource.getJdbcTemplate(null), "back_up");
    }


    /**
     * 配置自定义JSON结果
     */
    @Bean
    public CustomJsonValueProvider customJsonValueProvider() {
        return new CustomJsonValueProvider();
    }

    /**
     * 配置分页获取方式
     */
    @Bean
    public PageProvider pageProvider() {
        return new CustomPageProvider();
    }

    /**
     * 自定义UI界面鉴权
     */
    @Bean
    public CustomUIAuthorizationInterceptor customUIAuthorizationInterceptor() {
        return new CustomUIAuthorizationInterceptor();
    }

    /**
     * 自定义请求拦截器（鉴权）
     */
    @Bean
    public CustomRequestInterceptor customRequestInterceptor() {
        return new CustomRequestInterceptor();
    }

    /**
     * 自定义SQL缓存
     */
    // @Bean
    public CustomSqlCache customSqlCache() {
        return new CustomSqlCache();
    }

    /**
     * 自定义函数
     */
    // @Bean
    public CustomFunction customFunction() {
        return new CustomFunction();
    }

    /**
     * 自定义方法扩展
     */
    // @Bean
    public CustomFunctionExtension customFunctionExtension() {
        return new CustomFunctionExtension();
    }

    /**
     * 自定义模块
     */
    // @Bean
    public CustomModule customModule() {
        return new CustomModule();
    }

    /**
     * 自定义脚本语言
     */
    // @Bean
    public CustomLanguageProvider customLanguageProvider() {
        return new CustomLanguageProvider();
    }

    /**
     * 自定义列名转换
     */
    // @Bean
    public CustomMapperProvider customMapperProvider() {
        return new CustomMapperProvider();
    }


}
